package com.asia5b.a5mall.goods.adapter

import android.net.Uri
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import android.text.TextUtils
import android.view.View
import android.widget.RatingBar
import android.widget.TextView
import com.asia5b.a5bmall.resource.ResString
import com.asia5b.a5mall.goods.R
import com.asia5b.a5mall.goods.activity.GoodsDetail2Activity
import com.asia5b.a5mall.goods.databinding.ItemGoodscommenAdapterBinding
import com.asia5b.a5mall.middleware.model.goods.Goods2CommenModel
import com.asia5b.a5mall.middleware.utils.DisplayUtils
import com.asia5b.a5mall.middleware.utils.ImageWatchHelper
import com.asia5b.a5mall.middleware.utils.to
import com.asia5b.a5mall.middleware.widget.GridSpacingItemDecoration
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.chad.library.adapter.base.viewholder.BaseViewHolder

/**
 * Created by zhangyuncai on 2020/1/10.
 */
class GoodsCommentsAdapter(list: ArrayList<Goods2CommenModel>) :
        BaseQuickAdapter<Goods2CommenModel, BaseDataBindingHolder<ItemGoodscommenAdapterBinding>>(R.layout.item_goodscommen_adapter, list) {
    override fun convert(helper: BaseDataBindingHolder<ItemGoodscommenAdapterBinding>, item: Goods2CommenModel) {
        val binding = helper.dataBinding!!
        binding.data = item

        val tv_goodsdetail_commensgrade = helper.getView<TextView>(R.id.tv_goodsdetail_commensgrade)
        if (item.gradeInt == 3) {
            tv_goodsdetail_commensgrade.text = ResString.get(R.string.好评)
        } else if (item.gradeInt == 2) {
            tv_goodsdetail_commensgrade.text = ResString.get(R.string.中评)
        } else if (item.gradeInt == 1) {
            tv_goodsdetail_commensgrade.text = ResString.get(R.string.差评)
        }

        helper.setGone(R.id.ll_goodsdetail_replay_container, TextUtils.isEmpty(item.replyString))
        if (!TextUtils.isEmpty(item.replyString)) {
            helper.setText(R.id.tv_goodsdetail_replay, item.replyString)
        }

        val rb_goodsdetail_shop_star = helper.getView<RatingBar>(R.id.rb_goodsdetail_shop_star)
        rb_goodsdetail_shop_star.rating = item.rating

        val tv_goodsdetail_starvalue = helper.getView<TextView>(R.id.tv_goodsdetail_starvalue)
        tv_goodsdetail_starvalue.text = String.format("%.1f", item.rating)

        //评论图片
        val recyclerView = helper.getView<RecyclerView>(R.id.rv_commens)
        val images = arrayListOf<String>()
        if (!(item.images.isNullOrEmpty())) {
            for (i in 0 until item!!.images!!.size) {
                images.add(item!!.images!![i])
                if (i >= 4) {
                    break
                }
            }
        }
        if (!images.isNullOrEmpty()) {
            recyclerView.visibility = View.VISIBLE
            val adapter2 = GoodsCommensItemAdapter(images)
            recyclerView.run {
                layoutManager = GridLayoutManager(context, 4)
                adapter = adapter2
                if (itemDecorationCount == 0) {
                    addItemDecoration(GridSpacingItemDecoration(4, DisplayUtils.dip2px(context, 5f), false))
                }
            }
            adapter2.setOnItemClickListener { adapter, view, position ->
                showGallay(images, position)
            }
        } else {
            recyclerView.visibility = View.GONE
        }


        //追评图片
        val rv_commens_again = helper.getView<RecyclerView>(R.id.rv_commens_again)
    }

    fun showGallay(images: ArrayList<String>, picPosition: Int) {
//        if (context is GoodsDetail2Activity) {
//            (context as GoodsDetail2Activity).showGallay(images.map { Uri.parse(it) }, picPosition)
//        }
        ImageWatchHelper.show(context = context,anyList = images,position = 0)
    }
}